package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.system.domain.StatusMessageDetailInfo;
import com.ruoyi.system.domain.TmcMaintenanceAttach;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TmcPropertyMaintenance;
import com.ruoyi.system.service.ITmcPropertyMaintenanceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维修记录Controller
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/system/maintenance")
public class TmcPropertyMaintenanceController extends BaseController {
    @Autowired
    private ITmcPropertyMaintenanceService tmcPropertyMaintenanceService;

    /**
     * 查询维修记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:maintenance:list')")
    @GetMapping("/list")
    public TableDataInfo list(TmcPropertyMaintenance tmcPropertyMaintenance) {
        startPage();
        List<TmcPropertyMaintenance> list = tmcPropertyMaintenanceService.selectTmcPropertyMaintenanceList(tmcPropertyMaintenance);
        return getDataTable(list);
    }
//查询附件
    @PostMapping("/{ID}")
    public TableDataInfo selectAttachById(@PathVariable("ID") Long ID) {
        List<TmcMaintenanceAttach> list = tmcPropertyMaintenanceService.selectAttachById(ID);
        return getDataTable(list);
    }


    /**
     * 导出维修记录列表
     */
//    @PreAuthorize("@ss.hasPermi('system:maintenance:export')")
    @Log(title = "维修记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TmcPropertyMaintenance tmcPropertyMaintenance) {
        List<TmcPropertyMaintenance> list = tmcPropertyMaintenanceService.selectTmcPropertyMaintenanceList(tmcPropertyMaintenance);
        ExcelUtil<TmcPropertyMaintenance> util = new ExcelUtil<TmcPropertyMaintenance>(TmcPropertyMaintenance.class);
        return util.exportExcel(list, "maintenance");
    }
//

    /**
     * 获取维修记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:maintenance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tmcPropertyMaintenanceService.selectTmcPropertyMaintenanceById(id));
    }

    /**
     * 新增维修记录
     */
    @Log(title = "维修记录")
    @PostMapping
    public Map add(@RequestBody HashMap map) {
        TmcPropertyMaintenance tmcPropertyMaintenance = new TmcPropertyMaintenance();
        tmcPropertyMaintenance.setPublicuserId((Integer) map.get("PublicUser_ID"));
        tmcPropertyMaintenance.setHouseinfoId((Integer) map.get("HouseInfo_ID"));
        tmcPropertyMaintenance.setDescription((String) map.get("Description"));
        tmcPropertyMaintenance.setExpectedtime((String) map.get("ExpectedTime"));
        int i = tmcPropertyMaintenanceService.insertTmcPropertyMaintenance(tmcPropertyMaintenance);
        HashMap map1 = new HashMap();
        map1.put("ID", tmcPropertyMaintenance.getId());
        map1.put("Status", 0);
        map1.put("ErrorMessage", "上报记录成功");
        return map1;
    }

    /**
     * 用戶维修记录
     */
    @PostMapping("/QueryUserMaintenanceList")
    public TableDataInfo QueryUserMaintenanceList(@RequestBody HashMap map) {
        TmcPropertyMaintenance tmcPropertyMaintenance = new TmcPropertyMaintenance();
        tmcPropertyMaintenance.setPublicuserId((Integer) map.get("ID"));
        tmcPropertyMaintenance.setHouseinfoId((Integer) map.get("HouseInfo_ID"));
        List<TmcPropertyMaintenance> tmcPropertyMaintenance1 = tmcPropertyMaintenanceService.QueryUserMaintenanceList(tmcPropertyMaintenance);
        return getDataTable(tmcPropertyMaintenance1);
    }

    /**
     * 用戶维修记录
     */
    @PostMapping("/app/QueryMaintenanceRecordInfo")
    public TmcPropertyMaintenance appQueryMaintenanceRecordInfo(@RequestParam HashMap map) {
        System.out.println(map);
        TmcPropertyMaintenance tmcPropertyMaintenance = tmcPropertyMaintenanceService.selectTmcPropertyMaintenanceID(Long.valueOf(map.get("maintenanceID") + ""));
        return tmcPropertyMaintenance;
    }
    /**
     * 用戶维修记录
     */
    @PostMapping("/QueryMaintenanceRecordInfo")
    public TmcPropertyMaintenance QueryMaintenanceRecordInfo(@RequestBody HashMap map) {
        System.out.println(map);
        TmcPropertyMaintenance tmcPropertyMaintenance = tmcPropertyMaintenanceService.selectTmcPropertyMaintenanceID(Long.valueOf(map.get("maintenanceID") + ""));
        return tmcPropertyMaintenance;
    }
    /**
     * 員工维修记录
     */
    @PostMapping("/QueryUserMaintenanceListByID")
    public TableDataInfo QueryUserMaintenanceListByID(@RequestParam HashMap map) {
        TmcPropertyMaintenance tmcPropertyMaintenance = new TmcPropertyMaintenance();
        tmcPropertyMaintenance.setClientId(Integer.valueOf(map.get("ID") + ""));
        List<TmcPropertyMaintenance> tmcPropertyMaintenance1 = tmcPropertyMaintenanceService.QueryUserMaintenanceListByID(tmcPropertyMaintenance);
        return getDataTable(tmcPropertyMaintenance1);
    }

    /**
     * 完成的維修記錄
     */
    @GetMapping("/CompleteMaintenance")
    public StatusMessageDetailInfo CompleteMaintenance(@RequestParam Long maintenanceID) {
        int a = tmcPropertyMaintenanceService.CompleteMaintenance(maintenanceID);
        StatusMessageDetailInfo statusMessageDetailInfo = new StatusMessageDetailInfo();
        statusMessageDetailInfo.setState(1+"");
        statusMessageDetailInfo.setErrorMessage("成功");
        return statusMessageDetailInfo;
    }

    /**
     * 新增项目附件
     */
    @ApiOperation(value = "文件上传")
    @Log(title = "项目附件", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @CrossOrigin
    public HashMap fileUpload(@RequestParam(value = "file") MultipartFile[] file, @RequestParam(value = "nId") Long nId
            , @RequestParam(value = "nType") Long nType,@RequestParam(value = "isVideo") int isVideo) {
        try {
            String profile = RuoYiConfig.getProfile();
            TmcMaintenanceAttach tmcMaintenanceAttach = new TmcMaintenanceAttach();
            for (int i = 0; i <file.length ; i++) {
                byte[] bytes = file[i].getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "//mId//" + nId);
                //具体文件
                Path path = Paths.get(profile + "//mId//" + nId + "//" + file[i].getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    if (true) {
                        tmcMaintenanceAttach.setAttachurl("profile/mId/" + nId + "/" + file[i].getOriginalFilename());
                        tmcMaintenanceAttach.setMaintenanceId(nId);
                        tmcMaintenanceAttach.setAttachtype(nType);
                        tmcMaintenanceAttach.setIsVideo(isVideo);
                        tmcPropertyMaintenanceService.insertTmcMaintenanceAttach(tmcMaintenanceAttach);
                    } else {
                        if (file1.isFile()) {
                            file1.delete();
                        }
                        return null;
                    }
                }
            }
            HashMap map = new HashMap();
            map.put("ID", tmcMaintenanceAttach.getId());
            map.put("Status", 0);
            map.put("ErrorMessage", "上传附件成功");
            return map;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
/**
     * 新增项目附件
     */
    @Log(title = "项目附件", businessType = BusinessType.INSERT)
    @PostMapping(value = "/appupload")
    @CrossOrigin
    public StatusMessageDetailInfo appUpload(@RequestParam MultipartFile file, @RequestParam(value = "nId") String  nId) {
        try {
            StatusMessageDetailInfo statusMessageDetailInfo = new StatusMessageDetailInfo();
            statusMessageDetailInfo.setState(1+"");
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile + "//mId//" + nId);
            //具体文件
            Path path = Paths.get(profile + "//mId//" + nId + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                statusMessageDetailInfo.setErrorMessage("文件上传失败,文件存在");
                return statusMessageDetailInfo;
            } else {
                Files.write(path, bytes);//创建文件
                if (true) {
                    TmcMaintenanceAttach tmcMaintenanceAttach = new TmcMaintenanceAttach();
                    tmcMaintenanceAttach.setAttachurl("profile/mId/" + nId + "/" + file.getOriginalFilename());
                    tmcMaintenanceAttach.setMaintenanceId(Long.valueOf(nId));
                    tmcMaintenanceAttach.setAttachtype(1L);
                    tmcPropertyMaintenanceService.insertTmcMaintenanceAttach(tmcMaintenanceAttach);
                    statusMessageDetailInfo.setErrorMessage("成功");
                    return statusMessageDetailInfo;
                } else {
                    if (file1.isFile()) {
                        file1.delete();
                    }
                    return null;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
